//
//  SQLiteManger.swift
//  FMDBdemo
//
//  Created by changhongliang on 2017/8/30.
//  Copyright © 2017年 richinfo. All rights reserved.
//

import UIKit
import FMDB

class SQLiteManger: NSObject {
    ///单利
    static let shareInstance: SQLiteManger = SQLiteManger()
    //数据库对象
    var db: FMDatabase?
    /// 队列
    var dbQueue: FMDatabaseQueue?
    func openDB(name:String){
        let path = name.cacheDir()
        //0.创建数据库
        db = FMDatabase(path: path)
        dbQueue = FMDatabaseQueue(path: path)
        //1.打开数据库
        //和sqlite3_open方法一样,没有就创建有就打开
        if !db!.open() {
            return
        }
        //2.创建表
        createTable()
    }
    
    private func createTable(){
        //1.定义SQL语句
        let sql = "CREATE TABLE IF NOT EXISTS T_Status( \n" +
            "statusId TEXT PRIMARY KEY, \n" +
            "statusText TEXT, \n" +
            "userId TEXT, \n" +
            "createDate TEXT NOT NULL DEFAULT (datetime('now', 'localtime')) \n" +
        "); \n"
        //2.执行SQL语句
        //注意: 在FMDB中除了查询之外的所有操作都统称为更新
        try! db!.executeUpdate(sql, values: nil)
    
    }
    
    
}
